package com.itheima.job.handle;

import com.itheima.job.async.AsyncSendEmail;
import com.itheima.job.dto.EmailDTO;
import com.itheima.job.exception.SystemException;
import com.itheima.job.result.Result;
import com.itheima.job.result.enums.ResultEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author byZhao
 * @motto Talk is cheap . Show me the code!
 * @modify 2021-11-27 14:47
 * @description
 *
 *   ① 异常捕获
 *   ② 异常处理
 *
 */
@ControllerAdvice
@ResponseBody
public class ExceptionHandle {


    private static final Logger logger= LoggerFactory.getLogger(ExceptionHandle.class);

    @Autowired
    AsyncSendEmail asyncSendEmail;


    /**
     *  捕获异常SystemException
     * @param e
     * @return
     */
    @ExceptionHandler(SystemException.class)
    public Result handleSystemException(Exception e){

        System.out.println(".......................................................................");
        // 处理异常
        // ① 记录异常日志 ：保留现场！
        logger.error("system exception:{}",e.getMessage());
        // ② 发送邮件给系统管理员 【调用邮件服务】 【耗时】 线程异步
        EmailDTO emailDTO = new EmailDTO();
        emailDTO.setTo("zhaoboying@itcast.cn");
        emailDTO.setSubject("【Emergency】:系统异常及时处理");
        emailDTO.setText("出问题了。。。。。");
        asyncSendEmail.asyncSendEmail(emailDTO);
        // ③ 用户提示
        return  Result.fail(ResultEnum.SYSTEM_EXCEPTION_MESSAGE);
    }

    // 。。。。。



}
